package logic

import (
	"activity_srv/activity"
	"activity_srv/internal/svc"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetRoutePlanLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetRoutePlanLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRoutePlanLogic {
	return &GetRoutePlanLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetRoutePlanLogic) GetRoutePlan(in *activity.RouteRequest) (*activity.RouteResponse, error) {
	// todo: add your logic here and delete this line
	// 调用百度地图API获取路线规划
	result, err := l.svcCtx.BaiduMap.GetRoutePlanByAddress(l.ctx, in.Origin, in.Destination, in.Region)
	if err != nil {
		l.Logger.Errorf("获取路线规划失败: %v", err)
		return &activity.RouteResponse{
			Message: "获取路线规划失败: " + err.Error(),
		}, err
	}

	// 直接使用结构体字段，不需要检查API状态
	// 因为GetRoutePlanByAddress已经处理了错误情况

	// 转换响应格式
	response := &activity.RouteResponse{
		Distance:            int32(result.Distance),
		Duration:            int32(result.Duration),
		Message:             result.Message,
		OriginLocation:      result.OriginLocation,
		DestinationLocation: result.DestinationLocation,
	}

	for _, step := range result.Steps {
		response.Steps = append(response.Steps, &activity.RouteStep{
			Distance:    int32(step.Distance),
			Duration:    int32(step.Duration),
			Instruction: step.Instruction,
			Path:        step.Path,
		})
	}

	return &activity.RouteResponse{}, nil
}
